//
//  CHTabbar.swift
//  yzb
//
//  Created by cyptt on 2021/10/22.
//

import UIKit

typealias tabBarClickB = () -> Void
class CHTabbar: UITabBar {

    var mBulgeButton:CHButton?

    var barItemCount:Int = 0
    
   
   public var tabBarClickBlock:tabBarClickB?
    
    
    /**
     图标
     */
    public func bulgeButton(normalImage:UIImage,selectImage:UIImage,count:Int,tabBarCliB:@escaping tabBarClickB){
        let button:CHButton = CHButton.init();
      
        self.tabBarClickBlock = tabBarCliB
        self.mBulgeButton = button
    
        barItemCount = count
        
        self.mBulgeButton?.setImage(normalImage, for: UIControl.State.normal)
        self.mBulgeButton?.setImage(selectImage, for: UIControl.State.selected)
        
        self.mBulgeButton?.addTarget(self, action: #selector(btnClick), for: UIControl.Event.touchUpInside)
        self .addSubview(button)
    }
    
    @objc func btnClick(){
   
        if (self.tabBarClickBlock != nil) {
            self.tabBarClickBlock!()
        }
        
    }
    

    
    /**
     需要先执行bulgeButton方法
     */
    public func setButtonLable(msg:String,color:UIColor,fontSize:CGFloat){
        
        
        if (self.mBulgeButton != nil) {
            self.mBulgeButton?.setTitle(msg, for: UIControl.State.normal)
            self.mBulgeButton?.setTitleColor(UIColor.red, for: UIControl.State.normal)
            self.mBulgeButton?.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        }
    }
   
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        

        

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var index:Int = 0
        for subview  in self.subviews {
            
 
            let classString:String = String(describing: subview.self)
            if  classString.contains("UITabBarButton") {
        
                if index == (self.barItemCount-1)/2 {
                    
                    subview.isHidden = true
                }
                index += 1
            }
         
        }
        
        let itemW:CGFloat = self.bounds.size.width/CGFloat(self.barItemCount)
        let itemH:CGFloat = itemW
        
        self.mBulgeButton?.frame  = CGRect.init(x: 0, y: 0, width: itemW, height: itemH)
        self.mBulgeButton?.center = CGPoint.init(x: self.bounds.size.width/2, y: 0)
        
 
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   
    
    //判断点击是否在影响范围
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    
        
        if self.isHidden == false {
            let circle:UIBezierPath = UIBezierPath.init(arcCenter: self.mBulgeButton!.center, radius: (self.mBulgeButton!.frame.width )/2, startAngle:0 , endAngle: 2*CGFloat(Double.pi), clockwise: true)
            
            let tabbar:UIBezierPath = UIBezierPath.init(rect: self.bounds)
            if circle.contains(point) || tabbar .contains(point) {
                return true
            }
            return false
        }
        return super.point(inside: point, with: event)
    }
}



class CHButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var center:CGPoint = CGPoint.init()
        center.x = self.frame.size.width/2;
        center.y = self.frame.size.height/2
        self.imageView?.center = center;
        
       //text
        var newFrame:CGRect? = self.titleLabel?.frame
        newFrame?.origin.x = 0;
        newFrame?.origin.y = self.imageView!.frame.size.height/2 + self.frame.size.height/2 + 11 ;
        newFrame?.size.width = self.frame.size.width;
    
        self.titleLabel?.frame = newFrame!
        self.titleLabel?.textAlignment = NSTextAlignment.center;
    }
}
